package com.ray.depot.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.depot.service.StockInOutSerializeService;
import com.ray.depot.table.customMapper.SelectStockInOutSerializeMapper;
import com.ray.depot.table.dto.StockInOutSerializeDto;
import com.ray.depot.table.entity.StockInOutSerialize;
import com.ray.depot.table.entity.StockInOutSerializeExample;
import com.ray.depot.table.entity.StockInOutSerializeExample.Criteria;
import com.ray.depot.table.mapper.StockInOutSerializeMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class StockInOutSerializeServiceImpl extends BaseService implements StockInOutSerializeService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(StockInOutSerializeServiceImpl.class);
	
	@Autowired
	private StockInOutSerializeMapper stockInOutSerializeMapper;
	@Autowired
	private SelectStockInOutSerializeMapper selectStockInOutSerializeMapper;

	@Transactional
	public Result<Object> addStockInOutSerialize(StockInOutSerialize params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=stockInOutSerializeMapper.insertSelective(params);
		if(count!=1){
			logger.error("StockInOutSerializeServiceImpl=>addStockInOutSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public StockInOutSerialize queryStockInOutSerialize(String id, LoginUser user) {
		return stockInOutSerializeMapper.selectByPrimaryKey(id);
		}
	
	
    @Transactional
	public Result<Object> updateStockInOutSerialize(StockInOutSerialize params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=stockInOutSerializeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("StockInOutSerializeServiceImpl=>editStockInOutSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteStockInOutSerialize(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		StockInOutSerialize params=new StockInOutSerialize();
		params.setId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=stockInOutSerializeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("StockInOutSerializeServiceImpl=>deleteStockInOutSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<StockInOutSerialize> queryStockInOutSerialize(StockInOutSerialize params, PageDto<StockInOutSerialize> page, LoginUser user) {
		StockInOutSerializeExample example=new StockInOutSerializeExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(stockInOutSerializeMapper.countByExample(example));
		page.setRecords(stockInOutSerializeMapper.selectByExample(example));
		return page;
	}

    @Transactional(propagation=Propagation.NOT_SUPPORTED)
  	public PageDto<StockInOutSerializeDto> querySelectStockInOutSerialize(StockInOutSerializeDto params, PageDto<StockInOutSerializeDto> page, LoginUser user) {
  	    params.setTenantId(user.getTenantId());
  	    params.setOrderByClause(page.getOrderBy());
  	    params.setLimitStart((page.getPage() - 1)  * page.getPageSize());
  	    params.setLimitEnd(page.getPageSize());
  	    params.setIsDelete(DeleteEnum.USE.getValue());
  		page.setTotal(selectStockInOutSerializeMapper.countByExample(params));
  		page.setRecords(selectStockInOutSerializeMapper.selectByExample(params));
  		return page;
  	}
}
